package com.jack.house.utils;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

public class OssUtil {

    public static final String ALIYUN_KEY = "LTAIQjT7UfK9kVtY";

    public static final String ALIYUN_SECRET = "jk4KLKqzhikK1HXMzcyw9G7uRJt1Fj";

    public static final String END_POINT = "http://oss-cn-shanghai.aliyuncs.com";

    public static final String BUCKET = "sujinhe";

    public static final String FLODER = "house/";

    public static final String DOWNLOAD_CENTER = "download-center/";

    public static final String IMG_ACCESS_URL = "https://sujinhe.oss-cn-shanghai.aliyuncs.com/";


    public static final OSSClient client = new OSSClient(END_POINT, ALIYUN_KEY, ALIYUN_SECRET);

    // 自动剪裁 50x50: ?x-oss-process=image/resize,m_fill,h_50,w_50


    /**
     * 删除文件
     *
     * @param saveName
     * @throws OSSException
     * @throws ClientException
     */
    public static void deleteToDownloadCenter(String saveName) throws OSSException, ClientException {
        client.deleteObject(BUCKET, DOWNLOAD_CENTER+saveName);
    }



    /**
     * 上传文件
     *
     * @param file
     * @param saveName
     * @return
     * @throws OSSException
     * @throws ClientException
     * @throws IOException
     * @author felix  @date 2015-4-16 下午5:56:38
     */
    public static JSONObject uploadFile(MultipartFile file, String saveName) throws OSSException, ClientException, IOException {
        saveName = FLODER + saveName;
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(file.getSize());
        client.putObject(BUCKET, saveName, file.getInputStream(), meta);
        JSONObject result = new JSONObject();
        result.put("key", IMG_ACCESS_URL.concat(saveName));
        result.put("hash", IMG_ACCESS_URL.concat(saveName));
        result.put("bucket", FLODER);
        return result;
    }

    public static String uploadFile(File file, String saveName) throws OSSException, ClientException, IOException {
        saveName = FLODER + saveName;
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(file.length());
        PutObjectResult result = client.putObject(BUCKET, saveName, new FileInputStream(file), meta);
        System.out.println("the result is :::" + result.getETag() + ":::::" + result.toString());
        return result.getETag();
    }

    /**
     * 上传文件
     *
     * @param saveName
     * @return
     * @throws OSSException
     * @throws ClientException
     * @throws IOException
     * @author linweiqin  @date 2015-4-16 下午5:56:38
     */
    public static String uploadFile(URL urlObj, String saveName) throws OSSException, ClientException, IOException {
        saveName = FLODER + saveName;
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(urlObj.openStream().available());
        PutObjectResult result = client.putObject(BUCKET, saveName, urlObj.openStream(), meta);
        return result.getETag();
    }

    /**
     * 上传文件
     *
     * @param item
     * @param saveName
     * @return
     * @throws OSSException
     * @throws ClientException
     * @throws IOException
     * @author felix  @date 2015-4-16 下午5:58:47
     */
    public static String uploadFile(FileItem item, String saveName) throws OSSException, ClientException, IOException {
        saveName = FLODER + saveName;
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(item.getSize());
        PutObjectResult result = client.putObject(BUCKET, saveName, item.getInputStream(), meta);
        return result.getETag();
    }

    /**
     * 将bufferedImage图片上传到阿里云
     *
     * @param bf 图片流
     * @return
     * @throws IOException
     * @author ZYP 2016-11-9
     */
    public static String saveQrCodeImg(BufferedImage bf) throws IOException {
        //创建储存图片二进制流的输出流
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        //将bufferedImage写进输出流里，格式为png
        ImageIO.write(bf, "png", bs);
        //将二进制数据写进ByteArrayOutputStream
        InputStream is = new ByteArrayInputStream(bs.toByteArray());
        //上传到阿里云
        String saveName = java.util.UUID.randomUUID().toString() + ".png";
        ObjectMetadata meta = new ObjectMetadata();
        client.putObject(BUCKET, FLODER + saveName, is, meta);
        return saveName;
    }

}
